package com.hdjk.view.activity.financial;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.dialog.MyAlertDialog;
import com.hdjk.view.entity.CustomFundEntity;
import com.hdjk.view.network.CustomFundlNetWork;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 预约基金购买页面
 * Created by sxy on 2017/11/3.
 */

public class CustomFundBuyActivity extends BaseActivity implements View.OnClickListener {
    private CustomFundEntity mFund;
    private MyAlertDialog alertDialog;
    // Content View Elements

    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private TextView tv_name;
    private TextView tv_type;
    private TextView tv_profit;
    private TextView tv_earning;
    private ImageView iv_name;
    private EditText et_name;
    private ImageView iv_phone;
    private EditText et_phone;
    private RelativeLayout rel_bank;
    private ImageView iv_price;
    private EditText et_price;
    private TextView tv_buy;

    // End Of Content View Elements

    private void bindViews() {

        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_profit = (TextView) findViewById(R.id.tv_profit);
        tv_earning = (TextView) findViewById(R.id.tv_earning);
        iv_name = (ImageView) findViewById(R.id.iv_name);
        et_name = (EditText) findViewById(R.id.et_name);
        iv_phone = (ImageView) findViewById(R.id.iv_phone);
        et_phone = (EditText) findViewById(R.id.et_phone);
        rel_bank = (RelativeLayout) findViewById(R.id.rel_bank);
        iv_price = (ImageView) findViewById(R.id.iv_price);
        et_price = (EditText) findViewById(R.id.et_price);
        tv_buy = (TextView) findViewById(R.id.tv_buy);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_custom_fund_buy);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_buy.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        if (getIntent().getExtras().getSerializable("fund") instanceof CustomFundEntity) {
            mFund = (CustomFundEntity) getIntent().getExtras().getSerializable("fund");
            tv_name.setText(mFund.getFundName());
            tv_type.setText(mFund.getFundType() + " " + mFund.getFundCode());
            tv_earning.setText(mFund.getMinInitialIpoAmount());
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_buy:
                buy();
                break;
        }
    }

    private void buy() {
        String name = et_name.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShortToast(CustomFundBuyActivity.this, "请输入姓名");
            return;
        }
        String phone = et_phone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShortToast(CustomFundBuyActivity.this, "请输入电话号码");
            return;
        }
        String money = et_price.getText().toString();
        if (TextUtils.isEmpty(money)) {
            ToastUtils.showShortToast(CustomFundBuyActivity.this, "请输入购买金额");
            return;
        }

        mDialog.show();
        JSONObject obj = new JSONObject();
        try {
            obj.put("ProductId", mFund.getId());
            obj.put("Name", name);
            obj.put("Phone", phone);
            obj.put("Amount", money);
            CustomFundlNetWork.buyCustomFund(UserTokenUtil.getToken(CustomFundBuyActivity.this), obj.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    String title = "";
                    if (msg != null && msg.getSuccess()) {
                        title = "预约成功";
//                        back();
                    } else {
                        if (TextUtils.isEmpty(msg.getMsg())) {
                            title = "预约失败";
                        } else {
                            title = msg.getMsg();
                        }
                    }
                    final String finalTitle = title;
                    alertDialog = new MyAlertDialog(CustomFundBuyActivity.this, title, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (finalTitle.equals("预约成功")) {
                                back();
                            }
                        }
                    });
                    if (alertDialog != null)
                        alertDialog.show();
//                    ToastUtils.showShortToast(CustomFundBuyActivity.this, title);
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
